<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var minimum = function (nums) {
      const len = nums.length
      if (len < 2) return 0
      //建立两个hash表，分别统计奇偶下标下，各个元素出现的频率
      let map1 = new Map(), map2 = new Map();
      for (let i = 0; i < len; i++) {
        //判断奇偶
        if (i & 1) {
          //统计奇数下标下，各个元素出现的频率
          map1.has(nums[i]) ? map1.set(nums[i], map1.get(nums[i]) + 1) : map1.set(nums[i], 1)
        } else {
          //统计偶数下标下，各个元素出现的频率

          map2.has(nums[i]) ? map2.set(nums[i], map2.get(nums[i]) + 1) : map2.set(nums[i], 1)
        }
      }
      //将hash表变成一个二维数组
      let arr1 = [...map1.entries()]
      let arr2 = [...map2.entries()]
      //根据各个元素出现的频率，降序排序
      arr1.sort((a, b) => b[1] - a[1])
      arr2.sort((a, b) => b[1] - a[1])
      //最大值不相等
      if (arr1[0][0] !== arr2[0][0]) {
        return len - arr1[0][1] - arr2[0][1]
      } else {
        //最大值相等
        let sum1, sum2;
        //分类讨论，arr2中次最大值是否存在
        arr2[1] ? sum1 = arr1[0][1] + arr2[1][1] : sum1 = arr1[0][1]
        //分类讨论，arr1中次最大值是否存在
        arr1[1] ? sum2 = arr2[0][1] + arr1[1][1] : sum2 = arr2[0][1]
        //返回 操作数最小 的结果
        return Math.min(len - sum1, len - sum2)
      }
    };
    console.log(minimum(nums = [1, 2, 2, 2, 2]))
  </script>
</body>

</html>